import {isString,isNumber} from 'lodash'
export const getdate=(val,icon='-')=>{
	if(!val)return '';
    var unixtimestamp = new Date(val*1000);
    var year = 1900 + unixtimestamp.getYear();
    var month = "0" + (unixtimestamp.getMonth() + 1);
    var date = "0" + unixtimestamp.getDate();
    var hour = "0" + unixtimestamp.getHours();
    var minute = "0" + unixtimestamp.getMinutes();
    var second = "0" + unixtimestamp.getSeconds();
    return year + icon + month.substring(month.length-2, month.length)  + icon + date.substring(date.length-2, date.length)
};


export const getdateJson=(val)=>{
	if(!val)return '';
    var unixtimestamp = new Date(val*1000);
    var year = 1900 + unixtimestamp.getYear();
    var month = "0" + (unixtimestamp.getMonth() + 1);
    var date = "0" + unixtimestamp.getDate();
    return {year,month:month.substring(month.length-2, month.length),date:date.substring(date.length-2, date.length)}
};

export const getdate1=(val,icon='-')=>{
	if(!val)return '';
    var unixtimestamp = new Date(val*1000);
    var year = 1900 + unixtimestamp.getYear();
    var month = "0" + (unixtimestamp.getMonth() + 1);
    var date = "0" + unixtimestamp.getDate();
    var hour = "0" + unixtimestamp.getHours();
    var minute = "0" + unixtimestamp.getMinutes();
    var second = "0" + unixtimestamp.getSeconds();
    return year + icon + month.substring(month.length-2, month.length)  + icon + date.substring(date.length-2, date.length)+' '+hour.substring(hour.length-2, hour.length)+':'+minute.substring(minute.length-2, minute.length)+':'+second.substring(second.length-2, second.length)
};

export const formDate=()=>{
    let date = new Date(),
    Y = date.getFullYear(),
    M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1),
    D = date.getDate()>9?date.getDate():'0'+date.getDate();
    return Y+'-'+M+'-'+D;
}

export const formDateTime=()=>{
    let date = new Date(),
        Y = date.getFullYear(),
        M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1),
        D = date.getDate()>9?date.getDate():'0'+date.getDate(),
        h = date.getHours()>9?date.getHours():'0'+date.getHours(),
        m = date.getMinutes()>9?date.getMinutes():'0'+date.getMinutes(),
        s = date.getSeconds()>9?date.getSeconds():'0'+date.getSeconds();
    return Y+'-'+M+'-'+D+' '+h+':'+m+':'+s;
}

export const checkFileType=(suffix)=>{
    if(!suffix) return '';
    let suffixType=suffix.split('.');
    suffixType=suffixType[suffixType.length-1]
    var type="";
    switch (suffixType) {
        case 'mp4': 
            type = 'mp4'; 
            break;
        case 'ppt': 
        case 'pptx':
        case 'doc':
        case 'docx':
        case 'pdf':
            type = 'office';
            break;
        case 'jpg':
        case 'jpeg':
        case 'png':
        case 'bmp':
        case 'gif':
            type = 'pic';
            break;
    }
    return type;
}


export const showImage= (id)=>{
	var imglist=document.getElementById(id).querySelectorAll('img');
	for(var i=0;i<imglist.length;i++){
	    if(imglist[i].getAttribute('src')){
		if(imglist[i].getAttribute('src').indexOf("mmbiz.qpic.cn")!=-1){
			imglist[i].replace(/data-src/g, "src")
		}
	    }
	}
}



export const getfilesize=(size)=>{
    if (!size)
        return "";
    var num = 1024.00; //byte
    if (size < num)
        return size + "B";
    if (size < Math.pow(num, 2))
        return (size / num).toFixed(2) + "K"; //kb
    if (size < Math.pow(num, 3))
        return (size / Math.pow(num, 2)).toFixed(2) + "M"; //M
    if (size < Math.pow(num, 4))
        return (size / Math.pow(num, 3)).toFixed(2) + "G"; //G
    return (size / Math.pow(num, 4)).toFixed(2) + "T"; //T
}


export const RegObject=()=>{
    return {
        regMobild:/^[1][1,2,3,4,5,7,8][0-9]{9}$/,
        regId:/^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
        regEmail:/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/
    }
} 

export const getUserStatus=()=>{
    let userInfo=null;
    try{
        userInfo=localStorage.getItem('YF_USER_INFOS');
        userInfo=userInfo && JSON.parse(userInfo)?JSON.parse(userInfo):null
    }catch(e){console.log('user',e)}
    if(userInfo){
        if(!isString(userInfo.openid)){
            return -1;
        }else{
            return 1;
        }
    }
    return -1;
}

export const getUserRole=()=>{
    let userInfo=null;
    try{
        userInfo=localStorage.getItem('YF_USER_INFOS');
        userInfo=userInfo && JSON.parse(userInfo)?JSON.parse(userInfo):null
    }catch(e){console.log('user',e)}
    return userInfo?userInfo.status:0
}

